package com.gxuwz.Market.business.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gxuwz.Market.SystemContext;
import com.gxuwz.Market.business.dao.SysUserDAO;
import com.gxuwz.Market.business.entity.ServiceAcademy;
import com.gxuwz.Market.business.entity.ServiceBranch;
import com.gxuwz.Market.business.entity.ServiceClass;
import com.gxuwz.Market.business.entity.ServiceMajor;
import com.gxuwz.Market.business.entity.ServiceStudio;
import com.gxuwz.Market.business.entity.SysRoleRight;
import com.gxuwz.Market.business.entity.SysUser;
import com.gxuwz.Market.business.entity.SysUserRole;
import com.gxuwz.core.pagination.Result;

/**
 * <p>Title: 类名：用户-业务逻辑--实现</p>
 * <p>Description:mailsender-业务逻辑/n</p>
 * @author:	梧州学院 软件开发中心 庞光垚
 * @date:2015.07.31
 */

@Service("sysUserService")
public class SysUserServiceImpl implements ISysUserService {

	@Autowired
	private SysUserDAO sysUserDAO;
	@Autowired
	private ISysUserRoleService sysUserRoleService;
	/**
	 * 根据用户ID查找用户
	 */
	@Override
	public SysUser findByUserId(String userId){
		return sysUserDAO.findByUserId(userId);
	}
	/**
	 * 根据条件查找分页
	 */
	@Override
	public Result<SysUser> find(SysUser sysUser, int page, int size) {
		return sysUserDAO.find(sysUser, page, size);
	}
	/**
	 * 根据字段名和值查找用户
	 */
	@Override
	public List<SysUser> findByProperty(String property, String value) {
		return sysUserDAO.findByProperty(property, value);
	}
	@Override
	public void add(SysUser sysUser) {
		sysUser.setUserPassword(SystemContext.passwdEncryption(sysUser.getUserPassword()));
		sysUserDAO.save(sysUser);
		
	}
	@Override
	public void delete(String userId) {
		SysUser sysUser = findByUserId(userId);
		sysUserDAO.remove(sysUser);
	}
	@Override
	public void update(SysUser sysUser) {
		sysUserDAO.update(sysUser);
	}
	@Override
	public List<SysUser> findAll() {
		return sysUserDAO.findAll();
	}
	@Override
	public List<SysUser> findUserByClassName(String CLassname) {
		//System.out.print("zhe shi server");
		return sysUserDAO.findAllbyClassName(CLassname);
	}
	
	
	/**
	 * 用户登录
	 * @param sysUser 用户对象
	 * @return true -允许登录；false -不允许登录
	 */
	@Override
	public boolean isLogin(SysUser sysUser){
		boolean isL = false;//初始化临时变量
		SysUser sysUserTem = null;
		try {
			sysUserTem = sysUserDAO.findByProperty("userId", sysUser.getUserId()).get(0);
			if(null != sysUserTem){
				if(SystemContext.passwdDecryption(sysUser.getUserPassword(),sysUserTem.getUserPassword())){
					isL =true;
				}
			}
		} catch (Exception e) {
			System.out.println("isLogin error:"+e);
		}
		//isL = true;//测试
		
		return isL;
	}
	
	@Override
	public List<String> findUserPermissions(String userId) {
		List<String> strList=new ArrayList<String>();
		List<SysRoleRight> list=sysUserDAO.findUserPermissions(userId);
		for(int i=0;i<list.size();i++){
			strList.add(list.get(i).getRightId());
		}
		return strList;
	}
	
	
	/**
	 *查询所有的院系
	 * @author 黄森南
	 * @date 2018-1-25
	 * @return
	 */
	@Override
	public List<ServiceAcademy> findAllAcademy() {
		
		return sysUserDAO.findAllAcademy();
	}
	/**
	 *通过学院的Id查询学院的专业
	 * @author 黄森南
	 * @date 2018-1-25
	 * @return
	 */
	@Override
	public List<ServiceMajor> findByAca_Id(String userCollege) {
		
		return sysUserDAO.findByAca_Id(userCollege);
	}
	
	
	
	/**
	 *通过专业的Id查询学院的班级
	 * @author 黄森南
	 * @date 2018-1-25
	 * @return
	 */
	@Override
	public List<ServiceClass> findByMajorId(String userMajor) {
		
		return sysUserDAO.findByMajorId(userMajor);
	}
	
	@Override
	public List<ServiceClass> findAllClass() {
		
		return sysUserDAO.findClass();
	}
	
	
	/**
	 *查找研究室
	 * @author 黄森南
	 * @date 2018-1-25
	 * @return
	 */
	@Override
	public List<ServiceStudio> findStudio() {
		
		return  sysUserDAO.findStudio();
	}
	
	/**
	 *查询用户信息
	 * @author 黄森南
	 * @date 2018-1-25
	 * @return
	 */	
	
	@Override
	public Result findUser(int page,int row) {
		
		return sysUserDAO.findUser(page,row);
	}
	
	/**
	 * 通过学院的Id查找学院
	 * @author 黄森南
	 * @date 2018-1-29
	 * @return
	 */
	@Override
	public ServiceAcademy findAcayId(String academyId) {
		
		return sysUserDAO.findAcayId(academyId);
	}
	
	
	/**
	 * 通过专业的Id查找用户的专业
	 * @author 黄森南
	 * @date 2018-1-25
	 * @return
	 */
	@Override
	public ServiceMajor findMajById(String majorId) {
		
		return sysUserDAO.findMajById(majorId);
	}
	
	/**
	 * 修改密码
	 * @param userId
	 * @param userPassword
	 * @return
	 */
	@Override
	public int updateUserPassWd(String userId,String userPassword){
		int numT = 0;
		SysUser sysUser = sysUserDAO.findByProperty("userId", userId).get(0);
		if(null != sysUser){
			sysUser.setUserPassword(SystemContext.passwdEncryption(userPassword));
			sysUserDAO.update(sysUser);
			numT = 1;
		}
		return numT;
	}
	
	@Override
	public boolean checkUserId(String userId){
		boolean result = false;
		List<SysUser> sysUser = sysUserDAO.findByProperty("userId", userId);
		if(null != sysUser && sysUser.size()>0){
			result = true;
		}
		return result;
	}
	
	
	@Override
	public boolean checkPasswd(String rawPassword, String password) {
		return SystemContext.passwdDecryption(rawPassword, password);
	}
	
	
	@Override
	public void update(SysUser sysUser, String[] roleIds, String oldPassword) {
		//先根据用户编号删除用户角色
		sysUserRoleService.deleteUserRole(sysUser.getUserId());
		if(null != roleIds){
			//再循环添加用户角色
			for(int i = 0; i < roleIds.length; i++){
				SysUserRole sysUserRole = new SysUserRole();
				sysUserRole.setUserId(sysUser.getUserId());
				sysUserRole.setRoleId(roleIds[i]);
				sysUserRoleService.addUserRole(sysUserRole);
			}
		}
		
		if(sysUser.getUserPassword().equals("")){     //如果用户修改的密码为空，则是用户不修改密码，不用加密直接保存到数据库
			sysUser.setUserPassword(oldPassword);
		}else{
			//否则为已经修改了密码需要加密
			sysUser.setUserPassword(SystemContext.passwdEncryption(sysUser.getUserPassword()));
		}
		//更新用户信息
		sysUserDAO.update(sysUser);
	}
	
	/**
	 * 根据学院名称、专业名称、班级名称查找对应的用户信息
	 */
	@Override
	public List<SysUser> findSysUser(String academyName, String majorName, String ClassName ,String StudioName) {
		// TODO Auto-generated method stub
		return sysUserDAO.findSysUserByInfo(academyName, majorName, ClassName , StudioName);
	}
	
	/**
	 * 根据班级编号查找班级信息
	 */
	@Override
	public ServiceClass findClassInfoByClassId(String ClassId) {
		// TODO Auto-generated method stub
		return sysUserDAO.findClassById(ClassId);
	}
	
	@Override
	public ServiceStudio findStudioByStudioId(String StudioId) {
		// TODO Auto-generated method stub
		
		return sysUserDAO.findStudioByStudioId(StudioId);
	}
	@Override
	public List<SysUser> findUser(String userId) {
		// TODO Auto-generated method stub
		return sysUserDAO.findUser(userId);
	}
	@Override
	public List<SysUser> findByBranch(List<ServiceBranch> branchList) {
		// TODO Auto-generated method stub
		return sysUserDAO.findByBranch(branchList);
	}
}
	
	


